<?php
namespace app\api\controller\service;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Coupon extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 优惠券列表
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        $uid = $this->auth->id;
        $get = input('get.','','trim,strip_tags');
        if(isset($get['shop_id']) && $get['shop_id'] != '')
        {
            $where['shop_id'] = $get['shop_id'];
        }
        $where['is_check'] = 1;
        $where['type'] = ['<>',4];
        $where['starttime'] = ['<=',time()];
        $where['endtime'] = ['>=',time()];

        $list = model('app\api\model\service\Coupon')
            ->where($where)
            ->field('id,type,goods_id,shop_id,achieve,reduce,effective_day,starttime,endtime')
            ->order('id desc')
            ->select();
        foreach ($list as $key=>$value)
        {
            $exist = \app\api\model\service\UserCoupon::where(['user_id'=>$uid,'coupon_id'=>$value['id']])->field('id,state,exittime')->find();
            if($exist && $exist['exittime'] <=time())
            {
                unset($list[$key]);
            }
            $list[$key]['userState'] = $exist?1:0;
            $list[$key]['goodsName'] = $value['goods_id']?\app\api\model\service\Goods::where(['id'=>$value['goods_id']])->value('name'):'';
            $list[$key]['shopName'] = $value['shop_id']?\app\api\model\service\Shop::getName($value['shop_id']):'';
        }
        $list = array_values($list);
        $this->success('返回成功', $list);
    }

    /**
     * 领取优惠券
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function receive()
    {
        $uid = $this->auth->id;
        $type = input('type/d','');
        if($type == 0){
            $coupon_id = input('coupon_id/d','');
            $where['id'] = $coupon_id;
        }elseif ($type == 1){
            $code = input('code','');
            $where['code'] = $code;
        }
        $where['starttime'] = ['<',time()];
        $where['endtime'] = ['>',time()];
        $couponInfo = model('app\api\model\service\Coupon')
            ->where($where)
            ->find();
        !$couponInfo && $this->error('优惠券信息异常');
        $receiveExist = model('app\api\model\service\UserCoupon')->where(['coupon_id'=>$couponInfo['id'],'user_id'=>$uid])->count();
        $receiveExist > 0 && $this->error('请勿重复领取');
        \app\api\model\service\UserCoupon::create(['user_id'=>$uid,'coupon_id'=>$couponInfo['id'],'type'=>$couponInfo['type'],'goods_id'=>$couponInfo['goods_id'],'shop_id'=>$couponInfo['shop_id'],'code'=>$couponInfo['code'],'achieve'=>$couponInfo['achieve'],'reduce'=>$couponInfo['reduce'],'exittime'=>86400*$couponInfo['effective_day']+time(),'state'=>0]);
        if($type == 1)
        {
            \app\api\model\service\Coupon::where('id',$couponInfo['id'])->update(['endtime'=>time()]);
        }
        $this->success('领取成功');
    }

    /**
     * 用户优惠券列表
     * @return void
     */
    public function userCoupon()
    {
        $uid = $this->auth->id;
        $get = input('get.','','trim,strip_tags');
        $get['user_id'] = $uid;
        $list = \app\api\model\service\UserCoupon::getList($get);
        $this->success('信息返回成功',$list);
    }




}
